package com.andy.eqm.server.utils;

import java.util.List;

import org.apache.commons.lang3.RandomUtils;

import com.andy.eqm.server.entity.ConfProbability;
import com.google.common.collect.Lists;

/**
 * 结构：{30,50},{40,50}
 * @author cuiwm
 *
 */
public class ListTuple {
	
	private List<Tuple> list = Lists.newArrayList();
	private int total = 0;
	
	public ListTuple(List<ConfProbability> probabilities) {
		probabilities.forEach((it)->{
			list.add(new Tuple(it, it.getProbability()));
		});
		
		for(Tuple tu:list){
			total += tu.sec;
			tu.br = total;
		}
	}
	
	/**
	 * 通过随机权重获取值
	 * @param sec
	 */
	public Tuple selTuple(){
		return getTuple(RandomUtils.nextInt(0, total));
	}
	
	/**
	 * 通过权重获取值
	 * @param sec
	 */
	public Tuple getTuple(int sec){
		for(Tuple tu:list){
			if(tu.br > sec){
				return tu;
			}
		}
		return null;
	}
	
	public static class Tuple{
		private ConfProbability fir;
		private int sec;
		private int br;
		
		public Tuple(ConfProbability it,int sec) {
			this.fir = (it);
			this.sec = (sec);
		}

		public ConfProbability getFir() {
			return fir;
		}

		public int getSec() {
			return sec;
		}

		public int getBr() {
			return br;
		}

		public void setBr(int br) {
			this.br = br;
		}

		
	}
	
}

